import React, { Component } from 'react'
import axios from 'axios'
import PropTypes from 'prop-types'

export default class Search extends Component {
    state = {
        keywords:''
    }
    static propTypes = {
        updateAppState:PropTypes.func.isRequired
    }
    search = ()=>{

        this.props.updateAppState({isFirst:false,isLoading:true,list:[]})
        /*const {keywords} = this.state
        console.log('search==>',keywords)*/
        const {keywordsElement:{value}} = this
        console.log('searc fun2=>',value)
        axios.get('https://api.github.com/search/users?q='+value).then((res)=>{

            console.log(res.data.items)
            this.props.updateAppState({isFirst:false,isLoading:false,list:res.data.items})
            //this.props.updateAppState(res.data.items)
        }).catch((res)=>{
            this.props.updateAppState({isFirst:false,isLoading:false,err:res.message})
        })
    }
    //非受控组件
    getData = (inputName)=>{
        return (event)=>{
            console.log(event.target.value)
            this.setState({[inputName]:event.target.value})
        }
    }
    render() {
        return (
            <section className="jumbotron">
                <h3 className="jumbotron-heading">Search Github Users</h3>
                <div>
                {/*<input onChange={this.getData('keywords')} type="text" placeholder="enter the name you search"/>&nbsp*/}
                <input ref={c => this.keywordsElement = c} type="text" placeholder="enter the name you search"/>&nbsp;

                <button onClick={this.search}>Search</button>
                </div>
            </section>
        )
    }
}
